// Try This 2-2: a truth table for the logical operators.
// CHALLENGE: Modify to show values of 0 and 1, instead of false and true.
// John Kim, completed on Mon Jan  6 10:21:11 PST 2014 (with aid)
class LogicalOpTable {
    public static void main(String args[]) {

        boolean p, q;
        int i, j;
    
        // Shows true and false.
        System.out.println("P\tQ\tAND\tOR\tXOR\tNOT");
        
        p = true; q = true;
        System.out.print(p + "\t" + q + "\t");
        System.out.print((p&q) + "\t" + (p|q) + "\t");
        System.out.println((p^q) + "\t" + (!p));
        
        p = true; q = false;
        System.out.print(p + "\t" + q + "\t");
        System.out.print((p&q) + "\t" + (p|q) + "\t");
        System.out.println((p^q) + "\t" + (!p));
        
        p = false; q = true; 
        System.out.print(p + "\t" + q + "\t");
        System.out.print((p&q) + "\t" + (p|q) + "\t");
        System.out.println((p^q) + "\t" + (!p));
        
        p = false; q = false;
        System.out.print(p + "\t" + q + "\t");
        System.out.print((p&q) + "\t" + (p|q) + "\t");
        System.out.println((p^q) + "\t" + (!p));
        
        System.out.println();

        /* By switching to int, compiler will read 1 and 0
         * as true and false respectively. 
         * There was no need for casting. */
        System.out.println("P\tQ\tAND\tOR\tXOR\tNOT");
        
        i = 1; j = 1;
        System.out.print(i + "\t" + j + "\t");
        System.out.print((i&j) + "\t" + (i|j) + "\t");
        System.out.println((i^j) + "\t" + (1-i)); // Use of bitwise operators
        
        i = 0; j = 1;
        System.out.print(i + "\t" + j + "\t");
        System.out.print((i&j) + "\t" + (i|j) + "\t");
        System.out.println((i^j) + "\t" + (1-i));

        i = 1; j = 0;
        System.out.print(i + "\t" + j + "\t");
        System.out.print((i&j) + "\t" + (i|j) + "\t");
        System.out.println((i^j) + "\t" + (1-i));
        
        i = 0; j = 0;
        System.out.print(i + "\t" + j + "\t");
        System.out.print((i&j) + "\t" + (i|j) + "\t");
        System.out.println((i^j) + "\t" + (1-i));
    }
}
